package com.whfc.fvs.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.fvs.dao.FvsDeviceMapper;
import com.whfc.fvs.dao.FvsGroupMapper;
import com.whfc.fvs.dto.FvsGroupDTO;
import com.whfc.fvs.entity.FvsGroup;
import com.whfc.fvs.param.FvsGroupAddParam;
import com.whfc.fvs.param.FvsGroupEditParam;
import com.whfc.fvs.service.FvsGroupService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

import static com.whfc.common.constant.NameConst.DEFAULT_POUND_NAME;

/**
 * @author sun_guodong
 * @description
 * @date 2020-12-08
 */
@DubboService(interfaceClass = FvsGroupService.class, version = "1.0.0")
public class FvsGroupServiceImpl implements FvsGroupService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FvsGroupMapper fvsGroupMapper;

    @Autowired
    private FvsDeviceMapper fvsDeviceMapper;

    private static final String DEFAULT_GROUP_NAME = "默认组";

    @Override
    public List<FvsGroupDTO> list(Integer deptId) {
        logger.info("视频设备分组列表服务,deptId:{}", deptId);
        List<FvsGroupDTO> list = fvsGroupMapper.selectByDeptId(Collections.singletonList(deptId), null);
        if (list.size() == 0) {
            FvsGroup record = new FvsGroup();
            record.setDeptId(deptId);
            record.setName(DEFAULT_POUND_NAME);
            record.setEditFlag(0);
            fvsGroupMapper.insertSelective(record);
            list = fvsGroupMapper.selectByDeptId(Collections.singletonList(deptId), null);
        }

        return list;
    }

    @Override
    public List<FvsGroupDTO> list(List<Integer> deptIds) throws BizException {
        logger.info("公司下视频设备分组列表服务,deptIds:{}", deptIds);
        return fvsGroupMapper.selectByDeptId(deptIds, null);
    }

    @Override
    public void add(FvsGroupAddParam param) {
        logger.info("新增视频设备分组服务,param:{}", param.toString());
        FvsGroup record = new FvsGroup();
        record.setDeptId(param.getDeptId());
        record.setName(param.getName());
        fvsGroupMapper.insertSelective(record);
    }

    @Override
    public void edit(FvsGroupEditParam param) {
        logger.info("编辑视频设备分组服务,param:{}", param.toString());
        FvsGroup record = new FvsGroup();
        record.setId(param.getGroupId());
        record.setName(param.getName());
        fvsGroupMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void del(Integer groupId) {
        logger.info("删除视频设备分组服务,groupId:{}", groupId);
        Integer cnt = fvsDeviceMapper.countByGroupId(groupId);
        if (cnt > 0) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该分组下有设备,不能删除");
        }
        fvsGroupMapper.deleteLogic(groupId);
    }

    @Override
    public List<FvsGroupDTO> tree(Integer deptId, String keyword) {
        logger.info("查找视频设备的分组树服务,deptId:{},keyword:{}", deptId, keyword);
        List<FvsGroupDTO> list = fvsGroupMapper.selectByDeptId(Collections.singletonList(deptId), keyword);
        for (FvsGroupDTO fvsGroupDTO : list) {
            Integer groupId = fvsGroupDTO.getGroupId();
            Integer cnt = fvsDeviceMapper.countByGroupId(groupId);
            fvsGroupDTO.setDeviceNum(cnt);
        }
        return list;
    }
}
